package com.pai4j.pgc.controller.community;
import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.RequestPermission;
import com.pai4j.common.config.annotation.ValidateLogin;
import com.pai4j.common.enums.CommonStatusEnum;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.community.vo.request.CCExperienceReviewRequestVO;
import com.pai4j.domain.community.vo.request.CCExperienceSaveRequestVO;
import com.pai4j.domain.community.vo.response.cc.CTCExperienceDetailVO;
import com.pai4j.domain.community.vo.response.cc.CTCExperienceListVO;
import com.pai4j.pgc.service.community.CCExperienceService;
import com.pai4j.remote.pgc.community.CCExperienceClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *  技术社区经验专栏
 *
 * @author: CYM-pai
 * @date: 2025/10/27 13:53
 **/
@RestController
public class CCExperienceController implements CCExperienceClient {

    @Autowired
    private CCExperienceService experienceService;


    @ValidateLogin
    public PAIResponseBean<Long> save(@RequestBody CCExperienceSaveRequestVO experienceSaveRequest) {
        String userId = SessionHelper.getCurrentUserId();

        return PAIResponseBeanUtil.success(experienceService.save(experienceSaveRequest, userId));
    }

    @RequestPermission
    public PAIResponseBean<Boolean> review(@RequestBody CCExperienceReviewRequestVO experienceReviewRequest) {
        String userId = SessionHelper.getCurrentUserId();
        experienceService.review(experienceReviewRequest, userId);
        return PAIResponseBeanUtil.success(true);
    }

    @RequestPermission
    public PAIResponseBean<Boolean> updateFree(@PathVariable Long id) {
        String userId = SessionHelper.getCurrentUserId();
        experienceService.updateFree(id, userId);
        return PAIResponseBeanUtil.success(true);
    }

    /**
     * 设置置顶
     * @param id
     * @param status
     * @return
     */
    @RequestPermission
    public PAIResponseBean<Boolean> setTop(@PathVariable Long id, @PathVariable Boolean status) {
        String userId = SessionHelper.getCurrentUserId();
        experienceService.setTop(id, status, userId);
        return PAIResponseBeanUtil.success(true);
    }

    /**
     * 设置推荐
     * @param id
     * @param status
     * @return
     */
    @RequestPermission
    public PAIResponseBean<Boolean> setRecommend(@PathVariable Long id, @PathVariable Boolean status) {
        String userId = SessionHelper.getCurrentUserId();
        experienceService.setRecommend(id, status, userId);
        return PAIResponseBeanUtil.success(true);
    }

    @RequestPermission
    public PAIPageResponseBeanUtil<CTCExperienceDetailVO> adminPageList(@RequestParam(value = "kw", required = false) String kw,
                                                                        @RequestParam(value = "status", required = false) String status,
                                                                        @RequestParam(value = "pathId", required = false) Long pathId,
                                                                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        List<String> statusList = new ArrayList<>();
        if (StringUtils.isBlank(status)) {
            statusList = CommonStatusEnum.getAllEnableStatusDesc();
        } else {
            statusList.add(status);
        }
        return experienceService.adminPageList(kw, pathId, statusList, pageNo, pageSize);
    }

    /**
     * 最新经验列表查询
     */
    @Override
    public PAIPageResponseBeanUtil<CTCExperienceListVO> newPageList(@PathVariable Long pathId,
                                                                @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        String userId = SessionHelper.getCurrentUserId();
        return experienceService.newPageList(pathId, pageNo, pageSize, userId);
    }

    /**
     * 推荐经验列表查询
     */
    @Override
    public PAIPageResponseBeanUtil<CTCExperienceListVO> recommendPageList(@PathVariable Long pathId,
                                                                @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        String userId = SessionHelper.getCurrentUserId();
        return experienceService.recommendPageList(pathId, pageNo, pageSize, userId);
    }

    /**
     * 置顶经验列表查询
     */
    @Override
    public PAIPageResponseBeanUtil<CTCExperienceListVO> topPageList(@PathVariable Long pathId,
                                                                      @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        String userId = SessionHelper.getCurrentUserId();
        return experienceService.topPageList(pathId, pageNo, pageSize, userId);
    }

    /**
     * 经验详情查询
     * @param experienceId
     * @return
     */
    @Override
    public PAIResponseBean<CTCExperienceDetailVO> detail(@PathVariable Long experienceId,
                                                         @RequestHeader(value = "client", required = false, defaultValue = "web") String client) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(experienceService.getPermissionExperienceDetailFromCache(experienceId, userId, client));
    }

    /**
     * 经验删除
     * @param experienceId
     * @return
     */
    @ValidateLogin
    @Override
    public PAIResponseBean<Boolean> delete(@PathVariable Long experienceId) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(experienceService.delete(experienceId, userId));
    }

    @Override
    public PAIResponseBean<Set<String>> hotSigns(@PathVariable Long pathId,
                                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return PAIResponseBeanUtil.success(experienceService.getCCPathHotExperienceSignsFromCache(pathId, size));
    }

    @Override
    public PAIResponseBean<List<CTCExperienceListVO>> recommendList(@PathVariable Long pathId) {

        return PAIResponseBeanUtil.success(experienceService.getRecommendList(pathId));
    }
}
